package com.tony.distributedlock.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tony.distributedlock.mapper.StockMapper;
import com.tony.distributedlock.pojo.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author by 纪宗华
 * @Date 2024/3/5 14:13
 * @Description
 */
@Service
public class StockService {

//    private Stock stock = new Stock();
    @Autowired
    private StockMapper stockMapper;
    private ReentrantLock lock = new ReentrantLock();

    public void deduct() {
//        lock.lock();
        try {
            Stock stock = this.stockMapper.selectOne(new QueryWrapper<Stock>().eq("product_code","1001"));
            if (stock !=null && stock.getCount() > 0) {
                stock.setCount(stock.getCount() - 1);
                this.stockMapper.updateById(stock);
            }
//            System.out.println("库存余粮："+stock.getStock());
        } finally {
//            lock.unlock();
        }
    }
}
